#include "gwbase/base/test_harness.h"
#include <cassert>
#include <vector>

namespace dicom {
namespace {

struct Test {
  const char* base;
  const char* name;
  TestFunc test;
};

std::vector<Test> *g_tests = NULL;

}

bool RegisterTest(const char* base, const char* name, TestFunc test) {
  if (g_tests == NULL) {
    g_tests = new std::vector<Test>();
  }

  Test t;
  t.base = base;
  t.name = name;
  t.test = test;

  g_tests->push_back(t);
  return true;
}

void RunAllTests() {
  if (g_tests == NULL)
    return;

  const char* matcher = ::getenv("TESTS");

  int run = 0;
  for (size_t i = 0; i < g_tests->size(); ++i) {
    const Test& t = (*g_tests)[i];
    if (matcher != NULL) {
      std::string name = t.base;
      name.push_back('.');
      name.append(t.name);
      if (::strstr(name.c_str(), matcher) == NULL)
        continue;
    }

    fprintf(stderr, "==== Test %s.%s\n", t.base, t.name);
    t.test();
    ++run;
  }
  fprintf(stderr, "==== PASSED %d tests\n", run);
}

}
